using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public  class TraLoi_DAL
    {
         private SqlConnection cn = null;
        public  TraLoi_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(TraLoi_DTO Traloi)
        {
            SqlCommand cm = new SqlCommand("TraLoi_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID",Traloi.ID );
            cm.Parameters.AddWithValue("@MaKH",Traloi.MaKH );
            cm.Parameters.AddWithValue("@NoiDung", Traloi.NoiDung );
          
            cm.ExecuteNonQuery();
        }
        public void Update(TraLoi_DTO Traloi)
        {
            SqlCommand cm = new SqlCommand("TraLoi_Upsate", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", Traloi .ID );
            cm.Parameters.AddWithValue("@MaKH", Traloi .MaKH );
            cm.Parameters.AddWithValue("@NoiDung",Traloi .NoiDung );
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TraLoi_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@ID", iD);
            cm.ExecuteNonQuery();
        }
        public TraLoiCollection  Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TraLoi_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            TraLoiCollection  TraloiCollection = new TraLoiCollection ();

            while (dr.Read ())
            {
                TraLoi_DTO  Traloi = new TraLoi_DTO ();
                Traloi .ID  = int.Parse(dr.GetValue(0).ToString());
                Traloi .MaKH = int.Parse(dr.GetValue(1).ToString());
                Traloi .NoiDung  = dr.GetValue(2).ToString();
               
                TraloiCollection.Add(Traloi );
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return TraloiCollection;
        }
    }
}
